package com.easydict.core;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.SerializationConfig;

/**
 * 序列化上下文环境
 * 封装序列化过程中的环境信息和配置
 */
public class SerializationContext {
    
    private final SerializationConfig config;
    private final BeanDescription beanDescription;
    private final ClassLoader contextClassLoader;
    private final long timestamp;
    
    private SerializationContext(Builder builder) {
        this.config = builder.config;
        this.beanDescription = builder.beanDescription;
        this.contextClassLoader = builder.contextClassLoader;
        this.timestamp = System.currentTimeMillis();
    }
    
    public SerializationConfig getConfig() {
        return config;
    }
    
    public BeanDescription getBeanDescription() {
        return beanDescription;
    }
    
    public ClassLoader getContextClassLoader() {
        return contextClassLoader;
    }
    
    public long getTimestamp() {
        return timestamp;
    }
    
    public static Builder builder() {
        return new Builder();
    }
    
    public static class Builder {
        private SerializationConfig config;
        private BeanDescription beanDescription;
        private ClassLoader contextClassLoader;
        
        public Builder config(SerializationConfig config) {
            this.config = config;
            return this;
        }
        
        public Builder beanDescription(BeanDescription beanDescription) {
            this.beanDescription = beanDescription;
            return this;
        }
        
        public Builder contextClassLoader(ClassLoader contextClassLoader) {
            this.contextClassLoader = contextClassLoader;
            return this;
        }
        
        public SerializationContext build() {
            if (contextClassLoader == null) {
                contextClassLoader = Thread.currentThread().getContextClassLoader();
            }
            return new SerializationContext(this);
        }
    }
}
